
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       analogin.h
  * @author     baiyang
  * @date       2022-10-25
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "analogin_backend.h"
/*-----------------------------------macro------------------------------------*/
#define ANALOG_MAX_CHANNELS 16

// number of samples on each channel to gather on each DMA callback
#define ADC_DMA_BUF_DEPTH 8
/*----------------------------------typedef-----------------------------------*/
typedef struct AnalogSource* analog_source_t;

struct AnalogSource {
    struct AnalogSourceBackend analog_source_backend;

    // what value it has
    int16_t _pin;
    float _value;
    float _value_ratiometric;
    float _latest_value;
    uint8_t _sum_count;
    float _sum_value;
    float _sum_ratiometric;

    struct rt_mutex _mutex;
};

typedef struct AnalogIn* analogin_t;

struct AnalogIn {
    analog_source_backend_t _channels[ANALOG_MAX_CHANNELS];

    bool _initialized;
    float _board_voltage;
    float _servorail_voltage;
    float _rssi_voltage;
    uint16_t _power_flags;
    uint16_t _accumulated_power_flags;  // bitmask of all _power_flags ever set

    struct rt_mutex _mutex;

#if defined(HAL_WITH_MCU_MONITORING)
    float _mcu_temperature;
    float _mcu_voltage;
    float _mcu_voltage_min;
    float _mcu_voltage_max;
#endif
};

/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void analogin_init();
analog_source_backend_t analogin_channel(int16_t pin);
float analogin_board_voltage(void);
float analogin_servorail_voltage(void);
uint16_t analogin_power_status_flags(void);
uint16_t analogin_accumulated_power_status_flags(void);

#if defined(HAL_WITH_MCU_MONITORING)
float analogin_mcu_temperature(void);
float analogin_mcu_voltage(void);
float analogin_mcu_voltage_max(void);
float analogin_mcu_voltage_min(void);
#endif

/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



